﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using ContestMeter.Common;
using System.Windows;
using ContestMeter.UI;

namespace ContestMeter.Participants.UI
{
    public class ChangeNameViewModel : ViewModel<ChangeNameView>
    {
        [Dependency]
        public IFileSystem FileSystem { get; set; }

        [Dependency]
        public IConfiguration Configuration { get; set; }

        private Participant _participant;
        public Participant Participant
        {
            get { return _participant; }
            set
            {
                if (_participant != value)
                {
                    _participant = value;
                    OnPropertyChanged("Participant");
                }
            }
        }

        #region Save Command
        private DelegateCommand<object> _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand<object>(x => Save(), x => CanSave());
                }
                return _saveCommand;
            }
        }
        protected virtual void SaveCanExecuteChanged()
        {
            if (_saveCommand != null)
            {
                _saveCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion
        public virtual void Save()
        {
            if (Participant.IsValid)
            {
                Participant.Save(Configuration.SolutionsFolder);
                if (View != null)
                {
                    View.DialogResult = true;
                    View.Close();
                }
            }
            else
            {
                MessageBox.Show("Для продолжения работы нужно ввести данные во все обязательные поля.");
            }
        }
        protected virtual bool CanSave()
        {
            return !IsBusy;
        }


    }
}
